package com.kun.companion.core.ui.progress;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;

/**
 * 自定义进度条
 *
 * @author gzc
 * @since 2025/2/9
 **/
public class KunProgressBar extends BaseProgressBar {
    /**
     * 自定义渲染策略
     */
    private static final RenderStrategy renderStrategy = (canvas, width, height, progress) -> {
        GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.clearRect(0, 0, width, height);

        // 绘制背景
        gc.setFill(Color.rgb(240, 240, 240)); // 浅灰色背景
        gc.fillRoundRect(0, 0, width, height, height, height);

        // 绘制渐变进度条
        double progressWidth = width * progress;
        LinearGradient gradient = new LinearGradient(
                0, 0, 1, 0,
                true, CycleMethod.NO_CYCLE,
                new Stop(0.0, Color.DODGERBLUE),
                new Stop(1.0, Color.ORANGE)
        );
        gc.setFill(gradient);
        gc.fillRoundRect(0, 0, progressWidth, height, height, height);

        // 添加动态效果（例如：白色条纹）
        if (progress > 0) {
            gc.setStroke(Color.WHITE);
            gc.setLineWidth(2);
            for (double x = 0; x < progressWidth; x += 10) {
                gc.strokeLine(x, 0, x + 5, height);
            }
        }
    };

    /**
     * 基础构造器
     */
    public KunProgressBar() {
        super(0D, null, renderStrategy);
    }

}
